package com.example.fish.service.impl;

import com.example.fish.mapper.BlogMapper;
import com.example.fish.mapper.CommentMapper;
import com.example.fish.mapper.IllegalMapper;
import com.example.fish.pojo.*;
import com.example.fish.service.CommentService;
import com.example.fish.service.ex.BlogNotFindException;
import com.example.fish.service.ex.CommentDeleteException;
import com.example.fish.service.ex.CommentPublishException;
import com.example.fish.utils.SensitiveWordFilter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private BlogMapper blogMapper;
    @Resource
    private SensitiveWordFilter sensitiveWordFilter;
    @Resource
    private IllegalMapper illegalMapper;
    /***
     * 查询对应博客的评论信息
     * @param blogid 博客id
     * @return 全部评论数据
     */
    @Override
    public List<Comment> getCommentByBlogid(Integer blogid) {
//        根据id查询全部评论
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andBlogidEqualTo(blogid);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        return comments;
    }

    /***
     * 发布一级评论----有敏感字则替换为**
     * @param userid 当前登录的用户id
     * @param blogid 关于哪条博客的id
     * @param content 评论内容
     */
    @Override
    public void saveCommentOne(Integer userid, Integer blogid, String content) {
        Comment comment = new Comment();
        comment.setBlogid(blogid);
//        敏感字过滤
        String s = sensitiveWordFilter.filterSensitiveWord(content);
        comment.setContent(s);
//        违规+1
        boolean b = sensitiveWordFilter.containsSensitiveWord(content);
        if (b){
            Illegal illegal = new Illegal();
            illegal.setUserid(userid);
            illegalMapper.insert(illegal);
        }
        comment.setCommentuserid(userid);
//        时间
        // 创建 SimpleDateFormat 对象，指定日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
        // 获取当前时间
        Date date = new Date();
        // 格式化日期，并输出结果
        String formattedDate = sdf.format(date);
        comment.setCreatedate(formattedDate);
        comment.setIstop("1");
//        保存
        int insert = commentMapper.insert(comment);
        if (insert != 1){
            throw new CommentPublishException("发表评论失败");
        }
    }

    /***
     * 保存二级评论的方法
     * @param userid 发表的用户id
     * @param targetUserid 回复的用户id
     * @param whichCommentid 回复的哪条评论id
     * @param content 回复内容
     * @param blogid 属于哪条blog
     */
    @Override
    public void saveCommentTwo(Integer userid, Integer targetUserid, Integer whichCommentid, String content, Integer blogid) {
        Comment comment = new Comment();
        comment.setIstop("0");
        comment.setBlogid(blogid);
        comment.setCommentuserid(userid);
        comment.setTargetuserid(targetUserid);
        comment.setWhichcommentid(whichCommentid);
        String s = sensitiveWordFilter.filterSensitiveWord(content);
        comment.setContent(s);
        //        违规+1
        boolean b = sensitiveWordFilter.containsSensitiveWord(content);
        if (b){
            Illegal illegal = new Illegal();
            illegal.setUserid(userid);
            illegalMapper.insert(illegal);
        }
        // 创建 SimpleDateFormat 对象，指定日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
        // 获取当前时间
        Date date = new Date();
        // 格式化日期，并输出结果
        String formattedDate = sdf.format(date);
        comment.setCreatedate(formattedDate);
        int insert = commentMapper.insert(comment);
        if (insert != 1){
            throw new CommentPublishException("发表评论失败");
        }
    }

    @Override
    public PageInfo<UserComment> getCommentOneByUserid(Integer pageNum,Integer pageSize,Integer userid) {
//        查询所有user的评论，根据commentid查询对应的blog信息
        CommentExample commentExample=new CommentExample();
        commentExample.createCriteria().andCommentuseridEqualTo(userid);
        commentExample.setOrderByClause("createdate desc");
        PageHelper.startPage(pageNum,pageSize);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        int i = commentMapper.countByExample(commentExample);
        List<UserComment> list = new ArrayList<>();
        for (Comment comment : comments) {
            Blog blog = blogMapper.selectByPrimaryKey(comment.getBlogid());
            if (blog == null){
                continue;
//                throw new BlogNotFindException("查询blog时发生错误");
            }
//            封装UserComment实体类
            UserComment userComment = new UserComment();
            userComment.setCommentId(comment.getCommentid());
            userComment.setMyComment(comment.getContent());
            userComment.setTime(comment.getCreatedate());
//           blog相关的信息
            userComment.setTitle(blog.getTitle());
            userComment.setContent(blog.getContent());
            userComment.setBlogId(blog.getPostid());
            userComment.setUserid(blog.getUserid());
//            存入
            list.add(userComment);
        }
        PageInfo<UserComment> pageInfo =new PageInfo<>(list);
        pageInfo.setTotal(i);
        return pageInfo;
    }

    @Override
    public void deleteCommentByCommentid(Integer commentid) {
        System.out.println("删除评论"+commentid);
        int i = commentMapper.deleteByPrimaryKey(commentid);
        if (i == 0){
            throw new CommentDeleteException("删除失败！");
        }
    }
}
